<title>商品分类</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsGoodsCategory/GetIndex" lay-done="layui.data.done(d);">

</script>

<script type="text/html" id="LAY-app-CoreCmsGoodsCategory-tableBox-toolbar">
    <div class="layui-form coreshop-toolbar-search-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm layuiadmin-btn-admin" data-type="addData"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
            </div>
        </div>
    </div>
</script>


<div class="table-body">
    <table id="LAY-app-CoreCmsGoodsCategory-tableBox" lay-filter="LAY-app-CoreCmsGoodsCategory-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsGoodsCategory-tableBox-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coreHelper', 'treeTable'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , treeTable = layui.treeTable
                    , view = layui.view;

                //重载form
                form.render();

                // 渲染表格
                var insTb = treeTable.render({
                    id: 'treeTableBox',
                    elem: '#LAY-app-CoreCmsGoodsCategory-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsGoodsCategory/GetPageList',
                    where: { type: 'layui' },
                    method: 'POST',
                    tree: {
                        customName: {
                            id: 'id',
                            pid: 'parentId',
                            name: 'title',
                        },
                        view: {
                            showIcon: false,
                            indent: 25
                        },
                        data: {
                            //isSimpleData: true,
                        },
                        callback: {}
                    },
                    height: 'full-127',//无面包屑127,搜索框189,1行62
                    toolbar: '#LAY-app-CoreCmsGoodsCategory-tableBox-toolbar',
                    cols: [
                        [
                            { field: 'id', title: '序列', width: 60, sort: false },
                            { field: 'title', title: '名称', sort: false },
                            {
                                field: 'sort', title: '排序', sort: false, width: 105, templet: function (d) {
                                    return d.otherData.sort
                                }
                            },
                            {
                                field: 'imageUrl', title: '图片', width: 80, sort: false,
                                templet: function (d) {
                                    if (d.otherData.imageUrl) {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + d.otherData.imageUrl + '")><image style="max-width:28px;max-height:28px;" src="' + d.otherData.imageUrl + '" /></a>';
                                    } else {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + setter.noImagePicUrl + '")><image style="max-width:30px;max-height:30px;" src="' + setter.noImagePicUrl + '" /></a>';
                                    }
                                }
                            },
                            { field: 'isShow', title: '是否显示', width: 105, templet: '#switch_isShow', sort: false, unresize: true },
                            { width: 122, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsGoodsCategory-tableBox-bar' }
                        ]
                    ],
                    done: function (res, curr, count) {
                        treeTable.expandAll('treeTableBox', true);
                    },
                    page: false
                });

                //监听工具条
                treeTable.on('tool(LAY-app-CoreCmsGoodsCategory-tableBox)',
                    function (obj) {
                        if (obj.event === 'detail') {
                            doDetails(obj);
                        } else if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }
                    });

                $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });

                var active = {
                    addData: function () {
                        coreHelper.Post("Api/CoreCmsGoodsCategory/GetCreate", null, function (e) {
                            if (e.code === 0) {
                                admin.popup({
                                    shadeClose: false,
                                    title: '创建数据',
                                    area: ['600px', '540px'],
                                    id: 'LAY-popup-CoreCmsGoodsCategory-create',
                                    success: function (layero, index) {
                                        view(this.id).render('good/goodscategory/create', { data: e.data }).done(function () {
                                            //监听提交
                                            form.on('submit(LAY-app-CoreCmsGoodsCategory-createForm-submit)',
                                                function (data) {
                                                    var field = data.field; //获取提交的字段
                                                    field.isShow = field.isShow == 'on';

                                                    if (debug) { console.log(field); } //开启调试返回数据
                                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                    coreHelper.Post("Api/CoreCmsGoodsCategory/DoCreate", field, function (e) {
                                                        console.log(e)
                                                        if (e.code === 0) {
                                                            insTb.reloadData(); //刷新表格
                                                            layer.close(index); //再执行关闭
                                                            layer.msg(e.msg);
                                                        } else {
                                                            layer.msg(e.msg);
                                                        }
                                                    });
                                                });
                                        });
                                    }
                                    , btn: ['确定', '取消']
                                    , yes: function (index, layero) {
                                        layero.contents().find("#LAY-app-CoreCmsGoodsCategory-createForm-submit").click();
                                    }
                                });
                            } else {
                                layer.msg(e.msg);
                            }
                        });
                    }
                };

                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsGoodsCategory/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['600px', '540px'],
                                id: 'LAY-popup-CoreCmsGoodsCategory-edit',
                                success: function (layero, index) {
                                    view(this.id).render('good/goodscategory/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsGoodsCategory-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段
                                                field.isShow = field.isShow == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsGoodsCategory/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        insTb.reloadData(); //刷新表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsGoodsCategory-editForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                        coreHelper.Post("Api/CoreCmsGoodsCategory/DoDelete", { id: obj.data.id }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            insTb.reloadData(); //刷新表格
                            layer.msg(e.msg);
                        });
                    });
                }

                //监听 表格复选框操作
                layui.form.on('switch(switch_isShow)', function (obj) {
                    coreHelper.Post("Api/CoreCmsGoodsCategory/DoSetisShow", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        //table.reloadData('LAY-app-CoreCmsGoodsCategory-tableBox');
                        layer.msg(e.msg);
                    });
                });

            });
    };
</script>

<!--设置是否显示-->
<script type="text/html" id="switch_isShow">
    <input type="checkbox" name="switch_isShow" value="{{d.id}}" lay-skin="switch" lay-text="显示|关闭" lay-filter="switch_isShow" {{ d.otherData.isShow ? 'checked' : '' }}>
</script>